package com.vasthu.gui;

import com.vasthu.api.domain.User;
import com.vasthu.gui.utility.SpringUtilities;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public abstract class VstSelectionTableWindow extends JDialog implements VstTableCallBack {
              
    private JLabel caption;
    private JTable dataTable;
    private JButton okButton;
    private JButton cancelButton;

    public VstSelectionTableWindow() {
        initUi();
        initActions();
    }

    private void initUi() {
        setTitle("Select Item");
                
        caption = new JLabel("Select Item", JLabel.CENTER);
        caption.setFont(new Font("Monospaced 12", Font.BOLD, 16));
        dataTable = new JTable();
		dataTable.setModel(installTableModel());
        dataTable.setFont(new Font("Monospaced 12", Font.PLAIN, 12));
		dataTable.getTableHeader().setFont( new Font( "Arial" , Font.BOLD, 12 ));
		dataTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        okButton = new JButton("OK");
        okButton.setPreferredSize(new Dimension(100, 40));
        okButton.setMaximumSize(new Dimension(100, 40));
        cancelButton = new JButton("CANCEL");
        cancelButton.setPreferredSize(new Dimension(100, 40));
        cancelButton.setMaximumSize(new Dimension(100, 40));
        
        JPanel base = new JPanel(new SpringLayout());
        
        JPanel topPanel = new JPanel(new FlowLayout());
        topPanel.add(caption);
        topPanel.setPreferredSize(new Dimension(Integer.MAX_VALUE, 30));
        topPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
        base.add(topPanel);
        
        JScrollPane pane = new JScrollPane(dataTable);
        pane.setPreferredSize(new Dimension(Integer.MAX_VALUE, 300));
        base.add(pane);

        JPanel bPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));

        bPanel.setPreferredSize(new Dimension(Integer.MAX_VALUE, 40));
        bPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 40));
        bPanel.add(okButton);
        bPanel.add(Box.createRigidArea(new Dimension(10, 40)));
        bPanel.add(cancelButton);
        base.add(bPanel);
        
        SpringUtilities.makeCompactGrid(base,
                3, 1, //rows, cols
                10, 10, //initialX, initialY
                10, 10);
        
        add(base);
		
		setModalityType(ModalityType.APPLICATION_MODAL);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setMinimumSize(new Dimension(600, 500));
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screen.width - 600) / 2, (screen.height - 500) / 2);
        setVisible(false);
    }
	
	public int getSelectedRow() {
		return dataTable.getSelectedRow();
	}
	
	public int getSelectedColumn() {
		return dataTable.getSelectedColumn();
	}

	public TableModel getModel() {
		return dataTable.getModel();
	}
	
    private void initActions() {
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
				okButtonClicked();
            }
        });
        
        cancelButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
				close();
            }
        });
    }
	
	protected void setCaption(String title) {
		caption.setText(title);
	}
	
	public void rowDeleted(int row) {
		VstTableItemModel tModel = (VstTableItemModel)dataTable.getModel();
		tModel.deleteRow(row);
	}
	
	public void rowUpdated(int row, User updatedUser) {
		VstTableItemModel tModel = (VstTableItemModel)dataTable.getModel();
	}
	
	/** Will be overriden in each subclass */
	protected abstract VstTableItemModel installTableModel();
	
	/** Will be overriden in each subclass */
	protected abstract void okButtonClicked();
	
	protected void close() {
        dispose();
    }
}